﻿using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Build.Client;

namespace BuildExplorer
{
    internal static class TreeNodeTemplate
    {
        public static void ApplyTemplate(this TreeNode tn, IBuildInformationNode node, IBuildInformationNode containingAgentScope)
        {
            tn.Tag = node;

            tn.Text = node.GetNodeText();

            var duration = node.GetDuration();
            if (duration != null)
            {
                tn.Text += " " + duration.Value.ToString();
            }

            if (containingAgentScope == null)
            {
                tn.ForeColor = Color.MediumPurple;
            }
            else if (containingAgentScope.LastModifiedBy == node.LastModifiedBy)
            {
                // This node is under an AgentScope, but has the same user like the controller. Either agent runs as the same user
                // (maybe it shares machine with the controller), or this is some agent-management logic running on the controller
                tn.ForeColor = Color.Blue;    // This node definitely ran on the controller
            }

            if (node.Type == "BuildError")
            {
                tn.ForeColor = Color.Red;
                tn.BackColor = Color.FromArgb(0xFF, 0xFF, 0xE0, 0x80);
            }
            else if (node.Type == "BuildWarning")
            {
                tn.ForeColor = Color.Empty;
                tn.BackColor = Color.FromArgb(0xFF, 0xff, 0xE0, 0xE0);
            }

            string state;
            if (node.Fields.TryGetValue("State", out state) &&
                state == "Executing")
            {
                tn.BackColor = Color.FromArgb(0xFF, 0xE0, 0xE0, 0xFF);
            }
        }


        public static bool Contains(this TreeNode tn, string text)
        {
            var node = tn.Tag as IBuildInformationNode;
            if (node != null)
            {
                if (node.Type.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0)
                {
                    return true;
                }

                if (node.Fields.Any(kv => kv.Key.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0
                                       || kv.Value.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0))
                {
                    return true;
                }

                if (node.ActivityPropertiesNode() != null &&
                    node.ActivityPropertiesNode().Fields.Any(kv => kv.Key.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0 
                                                          || kv.Value.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0))
                {
                    return true;
                }

                if (node.BuildMessages().Any(n => n.Fields["Message"].IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0))
                {
                    return true;
                }
            }

            return false;
        }

        public static bool IsErrorOrExecuting(this TreeNode tn)
        {
            var node = tn.Tag as IBuildInformationNode;
            if (node != null)
            {
                string state;
                return (node.Fields.TryGetValue("State", out state) && state == "Executing")
                    || (node.Type == "BuildError");
            }

            return false;
        }
    }
}
